package com.tusuo.stds.ui.school;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.widget.ListView;
import android.widget.TextView;

import com.tusuo.stds.R;
import com.tusuo.stds.rest.AreaList;
import com.tusuo.stds.rest.SchoolList;
import com.tusuo.stds.rest.UserInfo;
import com.tusuo.stds.ui.BaseToolbarActivity;
import com.tusuo.stds.ui.ToolbarOwner;
import com.tusuo.stds.util.Utils;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.ColorRes;
import org.androidannotations.annotations.res.StringRes;
import org.parceler.Parcels;

import java.util.ArrayList;
import java.util.List;

import myandroid.library.util.ApplicationHelper;

@EActivity(R.layout.activity_area)
public class SchoolActivity extends BaseToolbarActivity<SchoolView, SchoolPresenter>
                                implements SchoolView {

    @ViewById(R.id.list_area_tv_name)
    TextView tvName;

    @ViewById(R.id.list_area)
    ListView listView;

    @StringRes(R.string.toolbar_title_set_school)
    String toolbarTitle;

    @ColorRes(R.color.colorPrimary)
    int toolbarBgColor;
    @ColorRes(R.color.textColorPrimary)
    int toolbarTextColor;

    private UserInfo mUserInfo;
    private int mAreaId;
    private String mAreaName;

    @AfterInject
    void init() {
        mUserInfo = Parcels.unwrap(getIntent().getParcelableExtra(UserInfo.EXTRA_NAME));
        mAreaId = getIntent().getIntExtra("areaId", 0);
    }

    @AfterViews
    void initData() {
        setupUI(this, toolbarBgColor, toolbarTextColor);

        List<ToolbarOwner.MenuAction> menuActionList = new ArrayList<>();

        ToolbarOwner.Config config = new ToolbarOwner.Config(toolbarTitle, true, R.drawable.ic_back_dark, menuActionList);
        mToolbarOwner.setConfig(config);

        mAreaName = getIntent().getStringExtra("name");

        tvName.setText(mAreaName);
    }

    @NonNull
    @Override
    public SchoolPresenter createPresenter() {
        return new SchoolPresenter(this, mUserInfo, mAreaId);
    }

    @Override
    protected void onResume() {
        super.onResume();
        presenter.loadData(mAreaId);
    }

    @Override
    public void setListViewAdapter(SchoolListAdapter adapter) {
        listView.setAdapter(adapter);
    }

    @ItemClick(R.id.list_area)
    void listItemClicked(SchoolList.School school) {
        if (Utils.UI.isFastDoubleClick()) {
            return;
        }

        presenter.setSchool(school);
    }
}
